package com.app.brilliant.game.controller;

import com.app.brilliant.common.api.ApiResult;
import com.app.brilliant.common.controller.BaseController;
import com.app.brilliant.common.exception.BusinessException;
import com.app.brilliant.sms.AliyunSmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 短信控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信相关接口")
public class SmsController extends BaseController {
    @Autowired
    private AliyunSmsService smsService;
    /**
     * 发送绑定手机号验证码
     * @param phone 手机号
     * @return 是否发送成功
     */
    @PostMapping("/sendBindPhoneCode")
    @ApiOperation("发送绑定手机号验证码")
    public ApiResult<Boolean> sendBindPhoneCode(@RequestParam String phone) {
        // 生成6位随机验证码
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        boolean ok = smsService.sendSms(phone, code, "bindPhone");
        return ApiResult.ok(ok);
    }

    /**
     * 绑定手机号
     * @param phone 手机号
     * @param code 验证码
     * @return 是否绑定成功
     */
    @PostMapping("/bindPhone")
    @ApiOperation("绑定手机号")
    public ApiResult<Boolean> bindPhone(@RequestParam String phone, @RequestParam String code) {
        if (!smsService.verifyCode(phone, code, "bindPhone")) {
            throw new BusinessException("验证码错误或已过期");
        }
        // 绑定手机号逻辑（此处需补充具体实现）
        return ApiResult.ok();
    }
}
